<template>
  <ContentWrap>
    <div :class="searchVisible ? 'flex flex-row flex-wrap mb-2' : 'flex flex-row flex-wrap'">
      <el-button v-if="showBack" @click="router.back()">
        <Icon icon="ep:arrow-left" class="mr-5px" />返回
      </el-button>
      <!-- <el-button
        plain
        type="primary"
        @click="openForm(undefined)"
        v-hasRole="['super_admin', 'platform_admin', 'tenant_admin']"
      >
        <Icon class="mr-5px" icon="ep:plus" />
        新增
      </el-button> -->
      <el-button plain type="success" @click="searchVisible = !searchVisible">
        <Icon class="mr-5px" icon="ep:operation" />
        {{ searchVisible ? '关闭筛选' : '打开筛选' }}
      </el-button>
      <el-text type="info">
        <span class="ml-5px">{{ searchVisible ? '关闭以下筛选条件' : '点击打开筛选条件' }}</span>
      </el-text>
      <div class="flex-1"></div>
      <el-button @click="resetQuery">
        <Icon class="mr-5px" icon="ep:refresh" />
        刷新列表
      </el-button>
    </div>
    <el-form v-if="searchVisible" ref="searchFormRef" :model="tableData.searchParam" inline>
      <!-- <el-form-item label="关键词" prop="keyword">
          <el-input
            v-model="tableData.searchParam.keyword"
            class="!w-240px"
            placeholder="请输入关键词"
            clearable
            @keyup="handleQuery"
          />
        </el-form-item> -->
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="tableData.searchParam.createTime"
          type="datetimerange"
          value-format="YYYY-MM-DD HH:mm:ss"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon class="mr-5px" icon="ep:search" />
          搜索
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>
  <ContentWrap>
    <div class="mt-[5px]">
      <el-table :data="tableData.list" size="large" v-loading="tableData.loading">
        <template #empty>
          <span>{{ !tableData.loading ? '没有相关数据' : '' }}</span>
        </template>
        <el-table-column label="用户编号" min-width="100">
          <template #default="{ row }">
            <div>{{ row.userId }}</div>
          </template>
        </el-table-column>
        <el-table-column label="交易单号" min-width="100">
          <template #default="{ row }">
            <div>{{ row.tradeNo }}</div>
          </template>
        </el-table-column>
        <el-table-column label="分佣比例，0-100之间的数字" min-width="100">
          <template #default="{ row }">
            <div>{{ row.bonusPercent }}</div>
          </template>
        </el-table-column>
        <el-table-column label="获得佣金，单位为分" min-width="100">
          <template #default="{ row }">
            <div>{{ row.bonusMoney }}</div>
          </template>
        </el-table-column>
        <el-table-column label="来源用户编号" min-width="100">
          <template #default="{ row }">
            <div>{{ row.sourceUserId }}</div>
          </template>
        </el-table-column>
        <el-table-column label="分佣基准金额，单位为分" min-width="100">
          <template #default="{ row }">
            <div>{{ row.basePrice }}</div>
          </template>
        </el-table-column>
        <el-table-column label="状态：0-冻结中、1-已解冻、2-已取消" min-width="100">
          <template #default="{ row }">
            <div>{{ row.status }}</div>
          </template>
        </el-table-column>
        <el-table-column label="解冻时间" min-width="100">
          <template #default="{ row }">
            <div>{{ row.unfreezeTime }}</div>
          </template>
        </el-table-column>
        <el-table-column label="取消时间" min-width="100">
          <template #default="{ row }">
            <div>{{ row.cancelTime }}</div>
          </template>
        </el-table-column>
        <el-table-column label="备注" min-width="100">
          <template #default="{ row }">
            <div>{{ row.note }}</div>
          </template>
        </el-table-column>
        <el-table-column
          label="创建时间"
          align="center"
          prop="createTime"
          :formatter="dateFormatter"
          min-width="170"
        />
        <el-table-column
          v-hasRole="['super_admin', 'platform_admin', 'tenant_admin']"
          label="操作"
          min-width="100"
          align="center"
          fixed="right"
        >
          <template #default="scope">
            <el-button link type="primary" @click="openForm('update', scope.row.id)">
              编辑
            </el-button>
            <el-button link type="danger" @click="handleDelete(scope.row.id)"> 删除 </el-button>
          </template>
        </el-table-column>
      </el-table>
      <Pagination
        v-model:limit="tableData.searchParam.pageSize"
        v-model:page="tableData.searchParam.pageNo"
        :total="tableData.total"
        @pagination="loadTableData"
      />
    </div>
  </ContentWrap>
</template>

<script lang="ts" setup name="RebateRecordList">
import * as RebateRecordApi from '@/api/promotion/rebate/RebateRecordApi'
import { FormInstance } from 'element-plus'
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
import { fenToYuan } from '@/utils'
import { dateFormatter } from '@/utils/formatTime'

const { t } = useI18n()
const message = useMessage()
const router = useRouter()

const searchVisible = ref(false)

const tableData = reactive({
  page: 1,
  limit: 10,
  total: 0,
  loading: true,
  list: [],
  searchParam: {
    pageNo: 1,
    pageSize: 10,
    createTime: []
  }
})

const loadTableData = () => {
  tableData.loading = true
  RebateRecordApi.getPage({
    ...tableData.searchParam
  })
    .then((res: any) => {
      tableData.loading = false
      tableData.list = res.list
      tableData.total = res.total
    })
    .catch(() => {
      tableData.loading = false
    })
}

const searchFormRef = ref<FormInstance>()

const handleQuery = () => {
  tableData.searchParam.pageNo = 1
  loadTableData()
}

const resetQuery = () => {
  searchFormRef.value?.resetFields()
  tableData.searchParam = {
    pageNo: 1,
    pageSize: 10,
    createTime: []
  }
  handleQuery()
}

const formRef = ref()
const openForm = (type: string, id?: number | string) => {
  formRef.value.open(type, id)
}

const handleDelete = async (id: number | string) => {
  try {
    await message.delConfirm()
    await RebateRecordApi.deleteById(id)
    message.success(t('common.delSuccess'))
    loadTableData()
  } catch {}
}

const showBack = ref(false)
onMounted(() => {
  handleQuery()
})
</script>
